Text Processing with Ruby (for Embach Mark) by Rob Miller

Text Processing with Ruby (for Embach Mark) by Rob Miller

Author:Rob Miller
Language: eng
Format: epub
Tags: Pragmatic Bookshelf
ISBN: 978-1-68050-070-7
Publisher: The Pragmatic Bookshelf, LLC (573151)


​ puts ​"Matches!"​ ​if​ word.match(​/\A[​​#{​letters​}​​]+\z/o​)

​ ​end​

Then, when running the script with time, we’ll see the following output:

​ $ ​time​​ ​​ruby​​ ​​o-modifier.rb​

​ letters() called

​ Matches!

​ Matches!

​ Matches!

​ Matches!

​ Matches!

​ Matches!

​ Matches!

​ Matches!

​ Matches!

​

​ real 0m1.031s

​ user 0m0.024s

​ sys 0m0.005s

We can see from the output that the letters method was called only once—when the first word was matched—and then not again. This resulted in the script taking just over one second to execute, rather than the previous nine seconds.

Of course, this is a rather extreme example; it’s rare that you’ll find yourself needing to interpolate the result of a calculation that takes this long into a regular expression, and on top of that to be doing it inside a loop. But if you do, it’s worth remembering the o modifier.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.